﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Resto.CostControl.Core.Query.SupplierModule.ViewObject;
using Enterprise.Framework.Frame.Architecture;
using Enterprise.Framework.DomainModel.Repositories;
using Resto.CostControl.Core.Domain.Model;
using Enterprise.Framework.DomainModel.Specifications;
using Enterprise.Framework.DomainModel;

namespace Resto.CostControl.Core.Query.SupplierModule.QueryObject
{
    public class SupplierQO : IApplicationQueryObject
    {
        private IQueryable<SupplierVO> GetSupplierVOSet()
        {
            IEntityRepository<Supplier> repository = RestoCostDomainSession.CurrentSession.CreateEntityRepository<Supplier>();

            IQueryable<SupplierVO> list = from o in repository.Objects
                                          where o.IsDelete == false
                                          orderby o.Name
                                          select new SupplierVO()
                                          {
                                              ID = o.ID,
                                              Code = o.Code,
                                              Name = o.Name,
                                              Address = o.Address,
                                          };

            return list;
        }

        public SupplierVO[] QuerySupplierVOByPager(ISpecification<SupplierVO> spec, PagerCondition pager, string orderKey, bool isAscending)
        {
            IQueryable<SupplierVO> list = GetSupplierVOSet();

            if (spec != null)
                list = list.Where(spec.GetExpression());

            IEnumerable<SupplierVO> list2 = RepositoryExtention.Query(list, pager, orderKey, isAscending);
            return list2.ToArray();
        }

        public SupplierVO[] QueryAllSupplierVO()
        {
            IQueryable<SupplierVO> list = GetSupplierVOSet();

            return list.ToArray();
        }
    }
}
